package com.example.demo1.service;

import com.example.demo1.pojo.dto.productCreateDto;
import com.example.demo1.pojo.product;
import com.example.demo1.repository.productRepository;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class productService {
    @Autowired
    private productRepository productRepository;

    public product createProduct(productCreateDto productCreateDto) {
        product product = new product();
        product.setName(productCreateDto.getName());
        product.setDescription(productCreateDto.getDescription());
        product.setPrice(productCreateDto.getPrice());
        product.setOriginalPrice(productCreateDto.getOriginalPrice());
        product.setStock(productCreateDto.getStock());
        product.setCategoryId(productCreateDto.getCategoryId());
        product.setBrand(productCreateDto.getBrand());
        product.setThumbnailUrl(productCreateDto.getThumbnailUrl());
        product.setImageUrls(productCreateDto.getImageUrls());
        product.setTags(productCreateDto.getTags());
        product.setWeight(productCreateDto.getWeight());

        return productRepository.save(product);
    }

    public List<product> getAllProducts() {
        return productRepository.findAll();
    }

    public Optional<product> getProductById(Integer id) {
        Optional<product> productOpt = productRepository.findById(id);
        if (productOpt.isPresent()) {
            product product = productOpt.get();
            product.setViewCount(product.getViewCount()+1);
            productRepository.save(product);
        }
        return productOpt;
    }

    public product updaetProduct(Integer id, @Valid productCreateDto productUpdateDto) {
        Optional<product> productOpt = productRepository.findById(id);
        if (!productOpt.isPresent()) {
            throw new RuntimeException("Product not found");
        }

        product product = productOpt.get();

        if (productUpdateDto.getName() != null) {
            product.setName(productUpdateDto.getName());
        }

        if (productUpdateDto.getDescription() != null) {
            product.setDescription(productUpdateDto.getDescription());
        }

        if (productUpdateDto.getPrice() != null) {
            product.setPrice(productUpdateDto.getPrice());
        }

        if (productUpdateDto.getOriginalPrice() != null) {
            product.setOriginalPrice(productUpdateDto.getOriginalPrice());
        }

        if (productUpdateDto.getStock() != null) {
            product.setStock(productUpdateDto.getStock());
        }

        if (productUpdateDto.getCategoryId() != null) {
            product.setCategoryId(productUpdateDto.getCategoryId());
        }

        if (productUpdateDto.getBrand() != null) {
            product.setBrand(productUpdateDto.getBrand());
        }

        if (productUpdateDto.getThumbnailUrl() != null) {
            product.setThumbnailUrl(productUpdateDto.getThumbnailUrl());
        }

        if (productUpdateDto.getImageUrls() != null) {
            product.setImageUrls(productUpdateDto.getImageUrls());
        }

        if (productUpdateDto.getTags() != null) {
            product.setTags(productUpdateDto.getTags());
        }

        if (productUpdateDto.getWeight() != null) {
            product.setWeight(productUpdateDto.getWeight());
        }
        return productRepository.save(product);
    }

    public void deleteProduct(Integer id) {
        Optional<product> productOpt = productRepository.findById(id);
        if (!productOpt.isPresent()) {
            throw new RuntimeException("Product not found");
        }

        product product = productOpt.get();
        product.setStatus(0);
        productRepository.save(product);

    }

    public List<product> getAvailableProducts() {
        return productRepository.findAvailableProducts();
    }
}
